<template>
  <div>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-form :model="passwordForm" :rules="rules" ref="ruleFormRef">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item prop="pass">
              <el-input placeholder="请输入旧密码" v-model="passwordForm.pass">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item prop="checkPass">
              <el-input
                placeholder="请输入新密码"
                v-model="passwordForm.checkPass"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item prop="ensurePass">
              <el-input
                placeholder="请确认密码"
                v-model="passwordForm.ensurePass"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="40">
          <el-col :span="14">
            <el-button type="primary" @click="changePassword"
              >确认修改</el-button
            >
          </el-col>
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //修改密码表单
      passwordForm: {
        pass: "",
        checkPass: "",
        ensurePass: "",
      },
      //表单验证规则
      rules: {
        pass: [
          {
            required: true,
            message: "请输入旧密码",
            trigger: "blur",
          },
          {
            min: 6,
            max: 16,
            message: "旧密码的长度在6~16个字符之间",
            trigger: "blur",
          },
        ],
        checkPass: [
          {
            required: true,
            message: "请输入新密码",
            trigger: "blur",
          },
          {
            min: 6,
            max: 16,
            message: "新密码的长度在6~16个字符之间",
            trigger: "blur",
          },
        ],
        ensurePass: [
          {
            required: true,
            message: "请确认密码",
            trigger: "blur",
          },
          {
            min: 6,
            max: 16,
            message: "确认密码的长度在6~16个字符之间",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    changePassword() {
      this.$refs.ruleFormRef.validate(async (valid) => {
        if (!valid) return;
        if (this.passwordForm.checkPass !== this.passwordForm.ensurePass) {
          this.$message.error("新密码与确认密码不符！");
        } else {
          const { data: res } = await this.$http.put();
          if (res.code !== 200) {
            this.$message.error("修改密码失败！");
          } else {
            this.$message.success("修改密码成功！");
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.el-row {
  margin-bottom: 20px;
}
</style>